/**
 * 
 */
package travibot.meta.dao.jdbctemplate;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;

import travibot.meta.StructureType;
import travibot.meta.dao.MaterialTypeDAO;
import travibot.utility.DescriptionNotFoundException;

/**
 * @author macekpet
 * 
 */
public class StructureTypeRowMapper implements RowMapper {

    private static final String ID = "ID_TYPE";
    private static final String NAME = "text";
    private static final String CLASS = "class";
    private static final String MAX_LEVEL_MAIN = "max_level_main";
    private static final String MAX_LEVEL_OTHER = "max_level_other";
    private static final String MATERIAL = "ID_MATERIAL";
    
    private static Logger log = Logger.getLogger(StructureTypeRowMapper.class);
    
    private MaterialTypeDAO materialTypeDao;

    public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {

        StructureType type = new StructureType();
        type.setId((Integer)rs.getObject(ID));
        type.setName(rs.getString(NAME));
        type.setImplementingClassName(rs.getString(CLASS));
        type.setMaxLevelMainVillage(rs.getInt(MAX_LEVEL_MAIN));
        type.setMaxLevel(rs.getInt(MAX_LEVEL_OTHER));
        int materialId = rs.getInt(MATERIAL);
        if(materialId > 0){
            try {
                type.setMaterial(materialTypeDao.getMaterialType(materialId));
            }
            catch (DescriptionNotFoundException e) {
                log.error(e);
            }
        }
        return type;
    }

    
    public void setMaterialTypeDao(MaterialTypeDAO materialTypeDao) {
    
        this.materialTypeDao = materialTypeDao;
    }

    
    
   
    
    

}
